﻿Imports System.IO
Imports System.Data.SqlClient
Public Class FormEmployeeAED

    Public groupId, proId, posId, userId, empId As Int16
    Public imgLocatoin, posName As String
    Dim photos As Image = My.Resources.FacebookNoPic
    Dim updatePhoto As Image
    Public emp As New EmployeeInfo


    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Dbinterface.OpenConnection()

        MyClear.ClearAllTeax(EmployeeGB)

        InitializeObject()
    End Sub
    Private Sub InitializeObject()
        For Each obj As Object In Groups.getGroup.Values
            cboGroup.Items.Add(obj)
        Next
        For Each obj As Object In Provinces.getProvince.Values
            cboProvince.Items.Add(obj)
        Next
        For Each obj As Object In Users.getUser.Values
            cboUser.Items.Add(obj)
        Next
    End Sub

    Private Sub SaveUpdate() 'save and update data

        Try

            'Passing values from object in class TextBox
            With emp
                .EmployeeCode = txtEmpCode.Text
                .EnglishName = txtEmpEngName.Text
                .KhmerName = txtEmpKhName.Text
                .Sex = cboSex.Text
                .DOB = txtDOB.Text
                .ProvinceId = proId
                .Address = txtAddress.Text
                .MaritalStatus = cboStatus.Text
                .IdCard = txtIdCard.Text
                .Phone = txtPhone.Text
                .Email = txtEmail.Text
                .StartDate = txtStartDate.Text
                .TypeOfContract = txtContract.Text
                .PositionId = posId
                .SalaryStartDate = txtSalaryStart.Text
                .GroupId = groupId
                .Note = txtNote.Text
                .Selection = userId
                .Blood = cboBlood.Text
                .Active = 1
                'Check image parth
                If imgLocatoin = "" Then

                    Dim MemStream As New MemoryStream
                    Dim DataPic_Update As Byte()
                    updatePhoto = pic.Image
                    Me.updatePhoto.Save(MemStream, Imaging.ImageFormat.Jpeg)
                    DataPic_Update = MemStream.GetBuffer()
                    MemStream.Read(DataPic_Update, 0, MemStream.Length)
                    .Photo = DataPic_Update
                Else
                    Dim fs As New FileStream(imgLocatoin.ToString, FileMode.Open, FileAccess.Read)

                    Dim data() As Byte = New Byte(fs.Length - 1) {}
                    fs.Read(data, 0, data.Length)
                    .Photo = data
                End If
               
                    If empId = Nothing Then
                        'Insert Employee

                        Employees.AddEmployee(emp)
                    Else
                        'Update Employee

                        Employees.UpdateEmployee(emp)

                    End If

            End With
            AddEmployeeToDataGridView()



        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

        Me.Close()

    End Sub

    Private Sub pic_Click(sender As Object, e As EventArgs) Handles pic.Click
        'Browse picture form local disk
        OpenFileDialog1.Title = "Select Your Photo"

        OpenFileDialog1.Filter = "image file(*.jpg, *.bmp, *.png) | *.jpg; *.bmp; *.png"

        If OpenFileDialog1.ShowDialog(Me) = DialogResult.OK Then

            pic.Image = Image.FromFile(OpenFileDialog1.FileName)

            imgLocatoin = OpenFileDialog1.FileName.ToString()
        End If
    End Sub

 

    Private Sub cboGroup_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboGroup.SelectedIndexChanged
        Dim group As GroupInfo = CType(cboGroup.SelectedItem, GroupInfo)
        groupId = group.GroupId
        'Get position by department
        SystemRole.getPosition(groupId, cboPosition, txtSalary.Text, posId)

    End Sub

   
    Private Sub cboPosition_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboPosition.SelectedIndexChanged
        'Get salary by position name.
        SystemRole.getSalary(cboPosition.Text, txtSalary.Text, posId)

    End Sub

  


    Private Sub cboProvince_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboProvince.SelectedIndexChanged
        Try
            'Get province from database into combobox
            Dim province As ProvinceInfo = CType(cboProvince.SelectedItem, ProvinceInfo)
            proId = province.ProvinceId

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

   

    Private Sub cboUser_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboUser.SelectedIndexChanged

        'Get user from database into combobox
        Dim user As UserInfo = CType(cboUser.SelectedItem, UserInfo)
        userId = user.UserId

    End Sub

    Private Sub btnSaveUpdate_Click(sender As Object, e As EventArgs) Handles btnSaveUpdate.Click

        IsValidation()
    End Sub

    Private Sub ButtonX1_Click(sender As Object, e As EventArgs) Handles btnClose.Click
        Me.Close()

    End Sub

    Public Sub AddEmployeeToDataGridView()
        Try
            'Check database connection
            Dim cn = Dbinterface.Connection
            If cn.State = ConnectionState.Closed Then

                Exit Sub

            End If
            'Passing vlues to  DataGridView in form Employee
            With FormEmployee.dgvEmployee.CurrentRow
                .Cells("EMPLOYEE_CODE").Value = emp.EmployeeCode
                .Cells("ENGLISH_NAME").Value = emp.EnglishName
                .Cells("KHMER_NAME").Value = emp.KhmerName
                .Cells("SEX").Value = emp.Sex
                .Cells("DOB").Value = emp.DOB
                .Cells("ADDRESS").Value = emp.Address
                .Cells("MARITAL_STATUS").Value = emp.MaritalStatus
                .Cells("ID_CARD").Value = emp.IdCard
                .Cells("PHONE").Value = emp.Phone
                .Cells("EMAIL").Value = emp.Email
                .Cells("START_DATE").Value = emp.StartDate
                .Cells("TYPE_OF_CONTRACT").Value = emp.TypeOfContract
                .Cells("SALARY_START_DATE").Value = emp.SalaryStartDate
                .Cells("BLOOD").Value = emp.Blood
                .Cells("MODIFY_BY").Value = cboUser.Text
                .Cells("PROVINCE_NAME").Value = cboProvince.Text
                .Cells("DEPARTMENT").Value = cboGroup.Text
                .Cells("POSITION").Value = cboPosition.Text
                .Cells("SALARY").Value = txtSalary.Text
                .Cells("PHOTO").Value = emp.Photo
                .Cells("NOTE").Value = emp.Note
                .Cells("ACTIVE").Value = emp.Active

                If empId = Nothing Then

                    .Cells(0).Value = emp.EmployeeId
                    'Refresh DataGridView with load data again from database 
                    RefreshData.DescendingRefresh(FormEmployee.dgvEmployee, 1)
                    FormEmployee.dgvEmployee.CurrentCell = FormEmployee.dgvEmployee.Rows(0).Cells(1)
                Else

                    FormEmployee.dgvEmployee.CurrentCell = FormEmployee.dgvEmployee.Rows(0).Cells(1)
                    Exit Sub
                End If

            End With
        Catch ex As Exception
            MsgBox(ex.Message)

        End Try
    End Sub


#Region "ValidationForm"

    Private Sub txtEmpEngName_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtEmpEngName.KeyPress
        '48 to 57 => 0 to 9
        '65 to 90 => A to Z
        '97 to 122 => a to z
        '8=> Backspace

        'Select Case Asc(e.KeyChar)
        '    Case 65 To 90, 97, 122, 8


        '    Case Else

        '        e.Handled = True
        'End Select
    End Sub

    Private Sub FormEmployeeAED_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        UI.SetPlaceHolder(txtEmpCode, "Ex: E001")
        UI.SetPlaceHolder(txtPhone, "Ex: 011 123 456 or 4567")
        UI.SetPlaceHolder(txtEmail, "Ex: admin@gmail.com")
        UI.SetPlaceHolder(txtDOB, "dd/mm/yyyy")
        UI.SetPlaceHolder(txtAddress, "Optional")
        UI.SetPlaceHolder(txtStartDate, "dd/mm/yyyy")
        UI.SetPlaceHolder(txtSalaryStart, "dd/mm/yyyy")
        UI.SetPlaceHolder(txtEmpEngName, "Your English name")
        UI.SetPlaceHolder(txtEmpKhName, "Your Khmer name")
        UI.SetPlaceHolder(txtNote, "Optional")
        UI.SetPlaceHolder(txtIdCard, "Optional")
        UI.SetPlaceHolder(txtContract, "Optional")
        UI.SetPlaceHolder(txtSalary, "00.0000")
    End Sub

    Public Sub IsValidation()

        If Me.txtEmpCode.Text.Length < 4 Or Me.txtEmpCode.Text.Length > 50 Then

            MessageValidation.MyValidation(Highlighter1, txtEmpCode, "Error Employee Code", "Employee Code must be between 4 to 50 characters lenght ")
            Exit Sub
        Else
            MessageValidation.ElseValidation(Highlighter1, txtEmpCode)
        End If
        '=================================================================
        If Me.txtEmpEngName.Text.Length < 2 Or Me.txtEmpEngName.Text.Length > 50 Then

            MessageValidation.MyValidation(Highlighter1, txtEmpEngName, "Error Employee Name", "Employee name must be between 2 to 50 characters lenght ")

            Exit Sub
        Else
            MessageValidation.ElseValidation(Highlighter1, txtEmpEngName)
        End If
        '==================================================================
        If Me.txtEmpKhName.Text.Length < 4 Or Me.txtEmpKhName.Text.Length > 50 Then

            MessageValidation.MyValidation(Highlighter1, txtEmpKhName, "Error Employee Name", "Employee name must be between 4 to 50 characters lenght ")

            Exit Sub
        Else
            MessageValidation.ElseValidation(Highlighter1, txtEmpKhName)
        End If
        '==================================================================
        If Me.cboSex.Text = "" Then

            MessageValidation.MyValidation(Highlighter1, cboSex, "Error Sex", "Please select sex in CoboBox")

            Exit Sub
        Else
            MessageValidation.ElseValidation(Highlighter1, cboSex)
        End If
        '==================================================================
        If Validator.IsDate(txtDOB.Text) = False Then
            MessageValidation.MyValidation(Highlighter1, txtDOB, "Error Date", "Please input in valid format")
            Exit Sub
        Else
            MessageValidation.ElseValidation(Highlighter1, txtDOB)

        End If
        '==================================================================
        If Validator.IsPhone(txtPhone.Text) = False Then
            MessageValidation.MyValidation(Highlighter1, txtPhone, "Error Phone Number", "Please your phone in valid format")
            Exit Sub
        Else
            MessageValidation.ElseValidation(Highlighter1, txtPhone)
        End If
        '==================================================================
        If Validator.IsEmail(txtEmail.Text) = False Then
            MessageValidation.MyValidation(Highlighter1, txtEmail, "Error Email", "Please your Email in valid format")
            Exit Sub
        Else
            MessageValidation.ElseValidation(Highlighter1, txtEmail)
        End If
        '==================================================================
        If Validator.IsDate(txtStartDate.Text) = False Then
            MessageValidation.MyValidation(Highlighter1, txtStartDate, "Error Date", "Please input in valid format")
            Exit Sub
        Else
            MessageValidation.ElseValidation(Highlighter1, txtStartDate)

        End If
        '==================================================================
        If Me.cboStatus.Text = "" Then

            MessageValidation.MyValidation(Highlighter1, cboStatus, "Error Status", "Please select status in CoboBox")

            Exit Sub
        Else
            MessageValidation.ElseValidation(Highlighter1, cboStatus)
        End If
        '==================================================================
        If Validator.IsDate(txtSalaryStart.Text) = False Then
            MessageValidation.MyValidation(Highlighter1, txtSalaryStart, "Error Date", "Please input in valid format")
            Exit Sub
        Else
            MessageValidation.ElseValidation(Highlighter1, txtSalaryStart)

        End If
        '===================================================================
        If Me.cboBlood.Text = "" Then
            MessageValidation.MyValidation(Highlighter1, cboBlood, "Error Blood", "Please select blood in ComboBox")
            Exit Sub
        Else
            MessageValidation.ElseValidation(Highlighter1, cboBlood)
        End If
        '=====================================================================
        If Me.cboUser.Text = "" Then
            MessageValidation.MyValidation(Highlighter1, cboUser, "Error Modify by", "Please select modify by in ComboBox")
            Exit Sub
        Else
            MessageValidation.ElseValidation(Highlighter1, cboUser)
        End If
        '=====================================================================
        If Me.cboGroup.Text = "" Then
            MessageValidation.MyValidation(Highlighter1, cboGroup, "Error Department", "Please select Department in ComboBox")
            Exit Sub
        Else
            MessageValidation.ElseValidation(Highlighter1, cboGroup)
        End If
        '=====================================================================
        If Me.cboPosition.Text = "" Then
            MessageValidation.MyValidation(Highlighter1, cboPosition, "Error Position", "Please select Position in ComboBox")
            Exit Sub
        Else
            MessageValidation.ElseValidation(Highlighter1, cboPosition)
        End If
        '=====================================================================
        If Me.cboProvince.Text = "" Then
            MessageValidation.MyValidation(Highlighter1, cboProvince, "Error Province", "Please select Province in ComboBox")
            Exit Sub
        Else
            MessageValidation.ElseValidation(Highlighter1, cboProvince)
        End If

        SaveUpdate()
    End Sub

#Region "Validation with MouseHover"
    Private Sub txtEmpEngName_Leave(sender As Object, e As EventArgs) Handles txtEmpEngName.Leave

        MessageValidation.ElseValidation(Highlighter1, txtEmpEngName)


    End Sub

    Private Sub txtEmpCode_Leave(sender As Object, e As EventArgs) Handles txtEmpCode.Leave
        MessageValidation.ElseValidation(Highlighter1, txtEmpCode)
    End Sub


    Private Sub txtEmpKhName_Leave(sender As Object, e As EventArgs) Handles txtEmpKhName.Leave

        MessageValidation.ElseValidation(Highlighter1, txtEmpKhName)


    End Sub
    Private Sub txtDOB_Leave(sender As Object, e As EventArgs) Handles txtDOB.Leave

        MessageValidation.ElseValidation(Highlighter1, txtDOB)

    End Sub


    Private Sub txtPhone_Leave(sender As Object, e As EventArgs) Handles txtPhone.Leave
        MessageValidation.ElseValidation(Highlighter1, txtPhone)
    End Sub

    Private Sub txtEmail_Leave(sender As Object, e As EventArgs) Handles txtEmail.Leave

        MessageValidation.ElseValidation(Highlighter1, txtEmail)


    End Sub


    Private Sub txtStartDate_Leave(sender As Object, e As EventArgs) Handles txtStartDate.Leave

        MessageValidation.ElseValidation(Highlighter1, txtStartDate)


    End Sub

    Private Sub txtSalaryStart_Leave(sender As Object, e As EventArgs) Handles txtSalaryStart.Leave
        MessageValidation.ElseValidation(Highlighter1, txtSalaryStart)
    End Sub
    Private Sub cboSex_Leave(sender As Object, e As EventArgs) Handles cboSex.Leave
        MessageValidation.ElseValidation(Highlighter1, cboSex)
    End Sub

    Private Sub cboStatus_Leave(sender As Object, e As EventArgs) Handles cboStatus.Leave
        MessageValidation.ElseValidation(Highlighter1, cboStatus)
    End Sub
    Private Sub cboBlood_Leave(sender As Object, e As EventArgs) Handles cboBlood.Leave
        MessageValidation.ElseValidation(Highlighter1, cboBlood)
    End Sub

    Private Sub cboUser_Leave(sender As Object, e As EventArgs) Handles cboUser.Leave
        MessageValidation.ElseValidation(Highlighter1, cboUser)
    End Sub
    Private Sub cboGroup_Leave(sender As Object, e As EventArgs) Handles cboGroup.Leave
        MessageValidation.ElseValidation(Highlighter1, cboGroup)
    End Sub
    Private Sub cboPosition_Leave(sender As Object, e As EventArgs) Handles cboPosition.Leave
        MessageValidation.ElseValidation(Highlighter1, cboPosition)
    End Sub
    Private Sub cboProvince_Leave(sender As Object, e As EventArgs) Handles cboProvince.Leave
        MessageValidation.ElseValidation(Highlighter1, cboProvince)
    End Sub
#End Region
#End Region

#Region "InsertValidation"
    Private Sub ValidationDataBaseInInsert()

        Dim Count As Int16
        MessageValidation.ValidationEmployeeCode(Count, txtEmpCode.Text)
        If Count > 0 Then
            MessageValidation.MyValidation(Highlighter1, txtEmpCode, "Duplicate with " + txtEmpCode.Text + "", "Employee code is already existed.")
            Exit Sub
        Else
            MessageValidation.ElseValidation(Highlighter1, txtEmpCode)
        End If
        '================================================================
        MessageValidation.ValidationEnglishName(Count, txtEmpEngName.Text)
        If Count > 0 Then
            MessageValidation.MyValidation(Highlighter1, txtEmpEngName, "Duplicate with " + txtEmpEngName.Text + "", "English Name is already existed.")
            Exit Sub
        Else
            MessageValidation.ElseValidation(Highlighter1, txtEmpEngName)
        End If
        '================================================================
        MessageValidation.ValidationKhmerName(Count, txtEmpKhName.Text)
        If Count > 0 Then
            MessageValidation.MyValidation(Highlighter1, txtEmpKhName, "Duplicate with " + txtEmpKhName.Text + "", "Khmer Name is already existed.")
            Exit Sub
        Else
            MessageValidation.ElseValidation(Highlighter1, txtEmpKhName)
        End If
        '================================================================
        MessageValidation.ValidationPhone(Count, txtPhone.Text)
        If Count > 0 Then
            MessageValidation.MyValidation(Highlighter1, txtPhone, "Duplicate with " + txtPhone.Text + "", "Phone number is already existed.")
            Exit Sub
        Else
            MessageValidation.ElseValidation(Highlighter1, txtPhone)
        End If
        '================================================================
        MessageValidation.ValidationEmail(Count, txtEmail.Text)
        If Count > 0 Then
            MessageValidation.MyValidation(Highlighter1, txtEmail, "Duplicate with " + txtEmail.Text + "", "Email is already existed.")
            Exit Sub
        Else
            MessageValidation.ElseValidation(Highlighter1, txtEmail)
        End If
        '================================================================
        MessageValidation.ValidationIdentification(Count, txtIdCard.Text)
        If Count > 0 Then
            MessageValidation.MyValidation(Highlighter1, txtIdCard, "Duplicate with " + txtIdCard.Text + "", "Identification is already existed.")
            Exit Sub
        Else
            MessageValidation.ElseValidation(Highlighter1, txtIdCard)
        End If
    End Sub
#End Region

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ValidationDataBaseInInsert()
    End Sub

 ​
    Private Sub EmployeeGB_Enter(sender As Object, e As EventArgs) Handles EmployeeGB.Enter

    End Sub
End Class